var htmlWebpachPlugin = require("html-webpack-plugin");
var extractTextWebpackPlugin = require("extract-text-webpack-plugin");
var webpack = require("webpack");
var config = require("./config/index");
module.exports = {
  entry: config.chunks,
  // 输出
  output: {
    // 输出的文件路径
    path: config.buildPath,
    // 输出的文件名
    filename: "js/[name].js",
    publicPath: config.devPublicPath
  },

  // loader配置
  module: {
    rules: [
      {
        test: /\.styl$/,
        use: extractTextWebpackPlugin.extract({
          fallback: "style-loader",
          use: [
            {
              loader: "css-loader"
            },
            {
              loader: "stylus-loader"
            }
          ]
        })
      },
      {
        test: /\.(pug|jade)$/,
        use: [
          {
            loader: "pug-loader"
          }
        ]
      },
      {
        test: /\.css$/,
        use: extractTextWebpackPlugin.extract({
          fallback: "style-loader",
          use: [
            {
              loader: "css-loader"
            }
          ]
        })
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)$/,
        use: [
          {
            // 当文件大小很小的时候，会将文件转为base64格式的字符串
            loader: "url-loader",
            options: {
              name: "css/fonts/[name].[ext]",
              limit: 8192 // 8kb
            }
          }
        ]
      },
      {
        test: /\.(jpg|png|gif|jpeg)$/,
        use: [
          {
            loader: "url-loader",
            options: {
              name: "img/[name].[ext]",
              limit: 8192 // 8kb
            }
          }
        ]
      }
    ]
  },

  // 插件
  plugins: config.htmls.concat([
    // 一个页面就需要new一个
    // new htmlWebpachPlugin({
    //   // 网页的标题
    //   title: "测试网页",
    //   //网页模板
    //   template: "./src/pages/index/index.html",
    //   filename: "[name].html"
    // }),
    // new htmlWebpachPlugin({
    //   // 网页的标题
    //   title: "测试网页",
    //   //网页模板
    //   template: "./src/pages/test/index.html",
    //   filename: "[name].html"
    // }),

    new extractTextWebpackPlugin("css/[name].css"),
    /**
     * 配置全局模块
     *  属性名是在js中的引用，属性值是模块啊
     *
     */
    new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "window.jQuery": "jquery",
      "window.$": "jquery"
    }),

    /**
     * 抽离公共模块
     */
    new webpack.optimize.CommonsChunkPlugin({
      name: "common",
      filename: "js/base.js"
    })

    // 压缩js
  ])
};
